package com.ht.web.personage;

import com.ht.bean.aduit.Characters;
import com.ht.bean.aduit.Charmodule;
import com.ht.bean.aduit.Module;
import com.ht.bean.json.JsonData;
import com.ht.bean.json.TreeMenu;
import com.ht.service.aduit.CharactersService;
import com.ht.service.aduit.CharmoduleService;
import com.ht.service.aduit.ModuleService;
import com.ht.util.Pager;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


/**
 * @author lrq
 * @date 2020/6/28 14:24
 */
@Controller
@RequestMapping("jurisdiction")
public class JurisdictionController {
    @Resource
    private ModuleService moduleService;
    @Resource
    private CharactersService charactersService;
    @Resource
    private CharmoduleService charmoduleService;
    @Resource
    private JsonData jsonData;

    //角色表数据
    @RequestMapping("data")
    @ResponseBody
    public JsonData voucher(@Param("limit")int limit , @Param("page")int page){
        Pager pager = new Pager();
        pager.setPageSize(limit);
        pager.setCurrPage(page);
        jsonData.setCount(charactersService.selCount());
        List<Characters> list = (charactersService.sybase(pager));
        jsonData.setData(list);
        return jsonData;
    }
    //去树状图
    @RequestMapping("toedit")
    public String toedit(HttpSession session,Integer id){
        session.setAttribute("jsid",id);
        return "systemsetting/jurisdicedit";
    }
    //树状图数据
    @RequestMapping("atreedata")
    @ResponseBody
    private List<TreeMenu> atreedata(HttpSession session){
        //获取当前角色要选定什么
        Integer jsid = (Integer) session.getAttribute("jsid");
        List<Integer> integerList = charmoduleService.selectModulle(jsid);
        //一级
        List<Module> max = moduleService.selectByZ(0);
        List<TreeMenu> list = new ArrayList<>();//一级全部数据
        for (Module module:max){
            TreeMenu treeMenu = new TreeMenu();//一级
            treeMenu.setTitle(module.getModulename());
            treeMenu.setId(module.getModuleid());
            treeMenu.setSpread(true);//默认展开
            //二级
            List<Module> two = moduleService.selectByZ(module.getModuleid());
            List<TreeMenu> list2 = new ArrayList();//二级全部数据
            for (Module module1:two){
                TreeMenu treeMenu1 = new TreeMenu();//二级
                treeMenu1.setTitle(module1.getModulename());
                treeMenu1.setId(module1.getModuleid());
                //如果他没有下级说明他是三级
                List<Module> zlist = moduleService.selectByZ(module1.getModuleid());
                if (zlist.size() == 0){
                    //判断是否选定
                    for (int j=0;j<integerList.size();j++){
                        if (integerList.get(j) == module1.getModuleid()){
                            treeMenu1.setChecked(true);
                        }
                    }
                }
                //三级
                List<Module> three = moduleService.selectByZ(module1.getModuleid());
                List<TreeMenu> list3 = new ArrayList();//三级全部数据
                for (Module module2:three) {
                    TreeMenu treeMenu2 = new TreeMenu();//三级
                    treeMenu2.setTitle(module2.getModulename());
                    treeMenu2.setId(module2.getModuleid());
                    //判断是否选定
                    for (int j=0;j<integerList.size();j++){
                        if (integerList.get(j) == module2.getModuleid()){
                            treeMenu2.setChecked(true);
                        }
                    }
                    list3.add(treeMenu2);//添加三级数据
                }
                treeMenu1.setChildren(list3);//封装三级数据到二级
                list2.add(treeMenu1);//添加二级数据
            }
            treeMenu.setChildren(list2);//封装二级数据到一级
            list.add(treeMenu);//添加一级数据
        }
        return list;
    }

    //添加
    @RequestMapping("add")
    @ResponseBody
    public void add(Integer id,HttpSession session){
        //获取角色id
        Integer jsid = (Integer) session.getAttribute("jsid");
        //找当前那个权限
        Module module = moduleService.selectByPrimaryKey(id);
        List<Module> zlist = moduleService.selectByZ(module.getModuleid());
        if (module.getRoot() == 0){//一级遍历下面的所有
            List<Module> two = moduleService.selectByZ(module.getModuleid());
            for (Module module1:two){
                List<Module> three = moduleService.selectByZ(module1.getModuleid());
                for (Module module2:three){
                    //添加权限操作
                    Charmodule charmodule = new Charmodule();
                    charmodule.setCharacterid(jsid);
                    charmodule.setModuleid(module2.getModuleid());
                    Charmodule jg = charmoduleService.select(charmodule);
                    //为空说明有
                    if (jg == null){
                        charmoduleService.insert(charmodule);
                    }
                }
            }
        }else if (zlist.size() == 0){//三级
            //添加权限操作
            Charmodule charmodule = new Charmodule();
            charmodule.setCharacterid(jsid);
            charmodule.setModuleid(module.getModuleid());
            Charmodule jg = charmoduleService.select(charmodule);
            //为空说明没有
            if (jg == null){
                charmoduleService.insert(charmodule);
            }
        }else {//二级遍历下面的所有
            List<Module> three = moduleService.selectByZ(module.getModuleid());
            for (Module module2:three){
                //添加权限操作
                Charmodule charmodule = new Charmodule();
                charmodule.setCharacterid(jsid);
                charmodule.setModuleid(module2.getModuleid());
                Charmodule jg = charmoduleService.select(charmodule);
                //为空说明没有
                if (jg == null){
                    charmoduleService.insert(charmodule);
                }
            }
        }
    }
    //删除
    @RequestMapping("del")
    @ResponseBody
    public void del(Integer id,HttpSession session){
        //获取角色id
        Integer jsid = (Integer) session.getAttribute("jsid");
        //找当前那个权限
        Module module = moduleService.selectByPrimaryKey(id);
        List<Module> zlist = moduleService.selectByZ(module.getModuleid());
        if (module.getRoot() == 0){//一级遍历下面的所有
            List<Module> two = moduleService.selectByZ(module.getModuleid());
            for (Module module1:two){
                List<Module> three = moduleService.selectByZ(module1.getModuleid());
                for (Module module2:three){
                    //删除权限操作
                    Charmodule charmodule = new Charmodule();
                    charmodule.setCharacterid(jsid);
                    charmodule.setModuleid(module2.getModuleid());
                    Charmodule jg2 = charmoduleService.select(charmodule);
                    if (jg2 != null){
                        charmoduleService.deleteByPrimaryKey(jg2.getCharmoduleid());
                    }
                }
            }
        }else if (zlist.size() == 0){//三级
            //删除权限操作
            Charmodule charmodule = new Charmodule();
            charmodule.setCharacterid(jsid);
            charmodule.setModuleid(module.getModuleid());
            Charmodule jg = charmoduleService.select(charmodule);
            charmoduleService.deleteByPrimaryKey(jg.getCharmoduleid());
        }else {//二级遍历下面的所有
            List<Module> three = moduleService.selectByZ(module.getModuleid());
            for (Module module2:three){
                ///删除权限操作
                Charmodule charmodule = new Charmodule();
                charmodule.setCharacterid(jsid);
                charmodule.setModuleid(module2.getModuleid());
                Charmodule jg = charmoduleService.select(charmodule);
                if (jg != null){
                    charmoduleService.deleteByPrimaryKey(jg.getCharmoduleid());
                }
            }
        }
    }
}
